//
//  LoginRequestProvider.m
//  SulitLoan
//
//  Created by 虞嘉伟 on 2025/2/26.
//  
  

#import "LoginRequestProvider.h"
#import "LoginRequest.h"
#import <MJExtension/MJExtension.h>

@implementation LoginModel
@end



@implementation LoginRequestProvider

+ (void)requestLoginWithPhoneNumber:(NSString *)number code:(NSString *)code success:(void(^)(LoginModel *model))success failure:(void(^)(YTKRequest *request))failure {
    LoginRequest *request = [LoginRequest new];
    request.howled = number;
    request.toyland = code;
#ifdef DEBUG
    // request.howled = User.user.account;
    // request.toyland = @"202406";
#endif
    [request startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        NSDictionary *dict = request.responseObject;
        if (![dict isKindOfClass:NSDictionary.class]) {
            !failure ?: failure(request);
            return;
        }
        if ([dict[@"weeps"] intValue] == 0) {
            // 成功
            LoginModel *model = [LoginModel mj_objectWithKeyValues:dict[@"bounding"]];
//            User *user = User.user;
//            user.phone = number;
//            user.token = model.ways;
//            [user synchronize];
            
            // 保存登录的时间
            [[NSUserDefaults standardUserDefaults] setDouble:NSDate.date.timeIntervalSince1970 forKey:@"lastLoginTime"];
            
            // 保存Token
            Class User = NSClassFromString(@"User");
            NSObject *user = [User performSelector:@selector(user)];
            [user performSelector:@selector(setPhone:) withObject:number];
            [user performSelector:@selector(setToken:) withObject:model.ways];
            [user performSelector:@selector(synchronize)];
            
            // 保存token
            // [NSUserDefaults.standardUserDefaults setObject:model.ways forKey:@"token"];
            !success ?: success(model);
        } else {
            !failure ?: failure(request);
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        !failure ?: failure(request);
    }];
}
@end
